import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import router from './router'
import store from './store'

NProgress.configure({ showSpinner: false }) // 不显示右边圆环

// 白名单
const whiteList = ['/login']

// 路由全局前置守卫
router.beforeEach((to, from, next) => {
  // 1. 显示进度条
  NProgress.start()
  // 2. 获取token
  const token = store.getters.token
  // 3. 判断是否有token
  if (token) {
    // 如果是去登录页 ，拦截去首页
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      if (!store.getters.userList.length) {
        // 3.3 获取用户信息
        // this.$store.dispatch('user/getUserList')
      }
      next()
    }
  } else {
    // 3.2 判断是否在白名单内
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

// 路由全局后置守卫

router.afterEach((to, from) => {
  // 1. 关闭进度条
  NProgress.done()
})
